<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="35vw" destroy-on-close @closed="$emit('closed')" :align-center="true"
			   :close-on-click-modal="false"  :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
			<el-form-item label="任务编号" prop="task_no">
				<el-input v-model="form.task_no" placeholder="请输入任务编号" clearable autocomplete="off" readonly></el-input>
			</el-form-item>
			<el-form-item label="验收状态" prop="check_status">
				<el-radio-group v-model="form.check_status">
					<el-radio label="1">通过</el-radio>
					<el-radio label="2">拒绝</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="拒绝原因" prop="reason" v-if="form.check_status == 2">
				<el-input v-model="form.reason" placeholder="请输入拒绝原因" type="textarea" :autosize="{ minRows: 2, maxRows: 20}" clearable autocomplete="off"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	components: {},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '验收',
				edit: '验收',
				show: '验收'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				check_status: "1",
				task_no: '',
				field:'check',
				value:''
			},
			//验证规则
			rules: {
				check_status: [
					{required: true, message: '请选择验收状态'}
				],
			},

		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.task.updateStatus.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.task_no = data.task_no
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			// Object.assign(this.form, data)
		}
	}
}
</script>

<style lang="scss">
</style>
